package com.nkym.wypan.storage.utils;/*
 * @ClassName FileUploadUtils
 * @Desc TODO
 * @Author 19637
 * @Date 2023/6/11 12:11
 * @Version 1.0
 */

import cn.hutool.core.date.DateUtil;

import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.UUID;

public class FileUploadUtils {

    public static final String SEPARATOR = SystemUtils.isWindows()?"\\":"/";
   private static final String basePath = System.getProperty("user.home")+SEPARATOR+"upload"+SEPARATOR+"data";

    /**
     * path  ===> basePath/年/月/日/随机名字.suffix
     *
     * @param filename
     * @return
     */
    public static String generatePath(String filename) {

        String suffix = getSuffix(filename);
        return new StringBuffer(basePath)
                .append(SEPARATOR)
                .append(DateUtil.thisYear())
                .append(SEPARATOR)
                .append(DateUtil.thisMonth() + 1)
                .append(SEPARATOR)
                .append(DateUtil.thisDayOfMonth())
                .append(SEPARATOR)
                .append(UUID.randomUUID())
                .append(".")
                .append(suffix).toString();

    }

    public static String generateChunkPath(String identifier, Integer chunkNumber) {
        return new StringBuffer(basePath)
                .append(SEPARATOR)
                .append(DateUtil.thisYear())
                .append(SEPARATOR)
                .append(DateUtil.thisMonth() + 1)
                .append(SEPARATOR)
                .append(DateUtil.thisDayOfMonth())
                .append(SEPARATOR)
                .append(identifier)
                .append(SEPARATOR)
                .append(UUID.randomUUID())
                .append(SEPARATOR)
                .append(chunkNumber).toString();

    }

    public static void main(String[] args) {
        System.out.println(SystemUtils.isWindows());
        System.out.println(SEPARATOR);
    }

    public static String getSuffix(String filename) {
        int index = filename.lastIndexOf(".");
        if (index == -1) {
            return "";
        }
        return filename.substring(index + 1);
    }
}
